package com.customers.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.customers.entity.MainForm;
import com.customers.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/selectAll")
public class SelectAllServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        List<MainForm> forms = null;
        try {
            CustomerService customerService = (CustomerService) request.getServletContext().getAttribute("CustomerService");
            String sql = "customerAll";
            forms = customerService.searchAll(sql);
//            forms = SelectAllService.searchAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //查询结束，返回结果
        String formjson = JSON.toJSONString(forms, JSONWriter.Feature.WriteNullStringAsEmpty);
        response.getWriter().print(formjson);

    }
}
